<?php
/**
 * 数据库连接
 *
 * @author ChaiChunyan
 * @version $Id: mypdo.cls
 */

 
/**
 * MyPdo MySQL的PDO操作类
 * 所有返回结果格式：
 * array( 'errorcode' => 0, 'errorinfo' => array(), 'result' => mixed );
 * 
 * @author ChaiChunyan
 * @link http://cn.php.net/manual/en/book.pdo.php
 */
class MyPdo extends PDO {

	/**
	 * PDO操作的PDOStatement
	 * 
	 * @var object
	 */
	private $stmt;

	/**
	 * 设置编码 
	 * 
	 * @param string $decode 
	 * @return void
	 */
	public function set_names( $decode = 'UTF8' ){
		$this->query( 'SET NAMES ' . $decode );
	}


	/**
	 * 仿mysql_query，select，增加参数$params
	 * 
	 * @param string $sql  SQL语句(不包括Limit部分)
	 * @param array $params 参数
	 * @param int $limit Limit时，Limit值 
	 * @param int $offset Limit时，偏移值
	 * @return array array( errorcode, errorinfo, result );
	 */
	public function select( $sql, $params = array(), $limit = null, $offset = null ){

		if( null != $limit ){
			$sql = sprintf( '%s LIMIT %d, %d', $sql, $offset, $limit );
		}

		$this->stmt = $this->prepare( $sql );
		$this->stmt->execute( $params );

		return $this->get_result( 'select' );
	}

	/**
	 * 执行select查询(用于单条结果查询) 
	 * 
	 * @param string $sql 
	 * @param array $params 
	 * @return array
	 */
	public function select_one( $sql, $params = array() ){
		$this->stmt = $this->prepare( $sql );
		$this->stmt->execute( $params );

		return $this->get_result( 'select_one' );

	}

	/**
	 * 获取结果 
	 * 
	 * @return array array( errorcode, errorinfo, result );
	 */
	private function get_result( $type = 'select' ){
		$result = array();
		$result['errorcode'] = (int)$this->stmt->errorCode();
		$result['errorinfo'] = $this->stmt->errorInfo();
		switch( $type ){
			case 'insert':		//返回本次insert的insertId
				$result['result'] = $this->lastInsertId();
				break;
			case 'update':	//返回受影响结果数
			case 'delete':	//返回受影响结果数
				$result['result'] = $this->stmt->rowCount();
				break;
			case 'select_one':
				$result['result'] = $this->stmt->fetch( PDO::FETCH_ASSOC );
				break;
			case 'select':	//默认认为是查询
			default:
				$result['result'] = $this->stmt->fetchAll( PDO::FETCH_ASSOC );
		}

		//$this->stmt->debugDumpParams();

		$this->stmt->closeCursor();

		return $result;
	}

	/**
	 * 添加一条记录，并返回insertid 
	 * 
	 * @param string $sql 
	 * @param array $params 
	 * @return array array( errorcode, errorinfo, result );
	 */
	public function insert( $sql, $params ){
		$this->stmt = $this->prepare( $sql );
		$this->stmt->execute( $params );
                
		return $this->get_result( 'insert' );
	}

	/**
	 * 删除操作 
	 * 
	 * @param string $sql 
	 * @param array $params 
	 * @return array array( errorcode, errorinfo, result )
	 */
	public function delete( $sql, $params = array() ){
		$this->stmt = $this->prepare( $sql );
		$this->stmt->execute( $params );

		return $this->get_result( 'delete' );
	}

	/**
	 * 更新操作 
	 * 
	 * @param string $sql 
	 * @param array $params 
	 * @return array array( errorcode, errorinfo, result )
	 */
	public function update( $sql, $params = array() ){
		$this->stmt = $this->prepare( $sql );
		$this->stmt->execute( $params );

		return $this->get_result( 'update' );
	}
	
	public function debug(){
		return $this->stmt->debugDumpParams();
	}
}

